﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.IO;
using System.Reactive.Linq;

namespace Projet_WPF.ViewModel
{
    class ObservationsViewModel : BaseViewModel
    {
        #region variables
        private Model.Patient _patient;
        private Model.Observation _selectedObservation;
        private bool _closeSignal;

        private DataAccess.Observation _dataAccessObservation;

        private ObservableCollection<Model.Observation> _listObservations;

        private bool _visibleRole;
        private Byte[] _selectedPicture;

        private string _comment;
        private DateTime _date;
        private int _weight;
        private int _bloodPressure;
        private ObservableCollection<string> _prescription;
        private ObservableCollection<Byte[]> _pictures;
        private string _pathPicture;
        private string _prescriptionValue;

        #endregion

        #region commandes
        private ICommand _addObservationCommand;
        private ICommand _addPrescriptionCommand;
        private ICommand _addImageCommand;
        private ICommand _terminateCommand;
        #endregion

        #region getter / setter

        public ICommand TerminateCommand
        {
            get { return _terminateCommand; }
            set { _terminateCommand = value; }
        }

        public ICommand AddObservationCommand
        {
            get { return _addObservationCommand; }
            set { _addObservationCommand = value; }
        }

        public ICommand AddPrescriptionCommand
        {
            get { return _addPrescriptionCommand; }
            set { _addPrescriptionCommand = value; }
        }

        public ICommand AddImageCommand
        {
            get { return _addImageCommand; }
            set { _addImageCommand = value; }
        }

        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        public bool VisibleRole
        {
            get { return _visibleRole; }
            set { _visibleRole = value; }
        }

        public Model.Patient Patient
        {
            get { return _patient; }
            set
            {
                if (_patient != value)
                {
                    _patient = value;
                    OnPropertyChanged("Patient");
                }
            }
        }

        public Byte[] SelectedPicture
        {
            get { return _selectedPicture; }
            set
            {
                if (_selectedPicture != value)
                {
                    _selectedPicture = value;
                    OnPropertyChanged("SelectedPicture");
                }
            }
        }

        public Model.Observation SelectedObservation
        {
            get { return _selectedObservation; }
            set
            {
                if (_selectedObservation != value)
                {
                    _selectedObservation = value;
                    OnPropertyChanged("SelectedObservation");
                }
            }
        }

        public ObservableCollection<Model.Observation> ListObservations
        {
            get { return _listObservations; }
            set
            {
                if (_listObservations != value)
                {
                    _listObservations = value;
                    OnPropertyChanged("ListObservations");
                }
            }
        }

        public string PrescriptionValue
        {
            get { return _prescriptionValue; }
            set
            {
                if (_prescriptionValue != value)
                {
                    _prescriptionValue = value;
                    OnPropertyChanged("PrescriptionValue");
                }
            }
        }

        public string Comment
        {
            get { return _comment; }
            set
            {
                if (_comment != value)
                {
                    _comment = value;
                    OnPropertyChanged("Comment");
                }
            }
        }

        public DateTime Date
        {
            get { return _date; }
            set
            {
                if (_date != value)
                {
                    _date = value;
                    OnPropertyChanged("Date");
                }
            }
        }

        public int Weight
        {
            get { return _weight; }
            set
            {
                if (_weight != value)
                {
                    _weight = value;
                    OnPropertyChanged("Weight");
                }
            }
        }

        public int BloodPressure
        {
            get { return _bloodPressure; }
            set
            {
                if (_bloodPressure != value)
                {
                    _bloodPressure = value;
                    OnPropertyChanged("BloodPressure");
                }
            }
        }

        public ObservableCollection<string> Prescription
        {
            get { return _prescription; }
            set
            {
                if (_prescription != value)
                {
                    _prescription = value;
                    OnPropertyChanged("Prescription");
                }
            }
        }

        public ObservableCollection<Byte[]> Pictures
        {
            get { return _pictures; }
            set
            {
                if (_pictures != value)
                {
                    _pictures = value;
                    OnPropertyChanged("Pictures");
                }
            }
        }

        public string PathPicture
        {
            get { return _pathPicture; }
            set
            {
                if (_pathPicture != value)
                {
                    _pathPicture = value;
                    OnPropertyChanged("PathPicture");
                }
            }
        }

        #endregion

        public ObservationsViewModel(Model.Patient patient, bool visible)
        {
            DisplayName = "Observations";
            Patient = patient;
            VisibleRole = visible;
            Weight = 0;
            BloodPressure = 0;
            Comment = "";
            Date = DateTime.Today;
            Prescription = new ObservableCollection<string>();
            Pictures = new ObservableCollection<byte[]>();
            PrescriptionValue = "";
            PathPicture = "";

            _dataAccessObservation = new DataAccess.Observation();

            ListObservations = new ObservableCollection<Model.Observation>(patient.Observations);
            AddObservationCommand = new RelayCommand(param => AddObservation());
            AddImageCommand = new RelayCommand(param => AddImage());
            AddPrescriptionCommand = new RelayCommand(param => AddPrescription());
            TerminateCommand = new RelayCommand(param => Terminate(), param => true);
        }

        private void Terminate()
        {
            CloseSignal = true;
        }

        private void AddObservation()
        {
            Model.Observation obs = Model.Observation.CreateObservation(Date, Comment, Prescription.ToArray(), Pictures.ToArray(), Weight, BloodPressure);
            if (_dataAccessObservation.AddObservation(_patient.Id, obs))
            {
                ListObservations.Add(obs);
                OnPropertyChanged("ListObservations");

                Date = DateTime.Today;
                Comment = "";
                Prescription = new ObservableCollection<string>();
                Pictures = new ObservableCollection<byte[]>();
                PrescriptionValue = "";
                PathPicture = "";
                Weight = 0;
                BloodPressure = 0;
            }
        }

        private void AddImage()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                StreamReader sr = new StreamReader(dlg.FileName);
                BinaryReader read = new BinaryReader(sr.BaseStream);
                Byte[] img = read.ReadBytes((int)sr.BaseStream.Length);
                PathPicture = dlg.FileName;
                Pictures.Add(img);
                OnPropertyChanged("Pictures");
            }
        }

        private void AddPrescription()
        {
            if (PrescriptionValue != "")
            {
                Prescription.Add(PrescriptionValue);
                OnPropertyChanged("Prescription");
                PrescriptionValue = "";
            }
        }
    }
}
